package cn.yitun.chat.yitunserverimapicore.business.chat.action;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.impl.HeadImpl;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunMapUtil;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.core.chat.LastChatGroup;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupChatContentDto;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.chat.service.GroupChatHistoryService;
import cn.yitun.chat.yitunserverimapicore.business.chat.service.LastChatGroupService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@ActionMapping(value = ActionTypeConstant.GROUP_CHAT_DATA_ACTION_PUSH)
public class GroupChatDataAction {

	@Resource
	private GroupChatHistoryService groupChatContentService;
	@Resource
	private LastChatGroupService lastChatGroupService;


	/**
	 * Date 2019-02-17 12:27:18
	 * Description 获取群聊记录 计数
	 * @param socketSession
	 * @param chatQuery
	 * @author yitun
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0001")
	public Object queryCount(
			SocketSession socketSession,
			@JsonDefine("data") GroupChatContentDto chatQuery) {
		if (NumUtil.isInValid(chatQuery.getGroupId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		return new ResultMessage(R.param("count", groupChatContentService.count(YitunMapUtil.java2Map(chatQuery, false))));
	}
	/**
	 * Date 2019-02-17 12:27:18
	 * Description 获取群聊记录 列表
	 * @param socketSession
	 * @param chatQuery
	 * @author yitun
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0002")
	public Object queryList(
			SocketSession socketSession,
			@JsonDefine("data") GroupChatContentDto chatQuery) {
		// String userId = socketSession.getKey();
		if (NumUtil.isInValid(chatQuery.getGroupId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

//		return groupChatContentService.queryGroupChatContentList(chatQuery);
		return new ResultMessage(R.param("list", groupChatContentService.listVO(YitunMapUtil.java2Map(chatQuery, false))));
	}
	/**
	 * Date 2019-02-17 12:27:18
	 * Description 获取群聊记录详情
	 * @param socketSession
	 * @param chatQuery
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0003")
	public Object getByContentId (
			SocketSession socketSession,
			@JsonDefine("data") GroupChatContentDto chatQuery) {

		return new ResultMessage(R.param("detail", groupChatContentService.getVO(chatQuery.getId())));
	}
	/**
	 * Date 2019-02-17 12:27:18<br>
	 * Description 获取群聊记录
	 * @param socketSession
	 * @param chatQuery
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0004")
	public Object getByContentIds(
			SocketSession socketSession,
			@JsonDefine("data") GroupChatContentDto chatQuery) {

		return groupChatContentService.queryByContentIds(chatQuery);
	}


	/**
	 * 
	 * Description 获取聊天记录（此方法主要以数据库id为条件） <br>
	 * Date 2019-05-25 08:04:55<br>
	 * 
	 * @param head
	 * @param query
	 * @return Object
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0006")
	public Object geChatList(HeadImpl head,
							 @JsonDefine("data") GroupChatContentDto query ) {
		if (NumUtil.isInValid(query.getGroupId()) || NumUtil.isInValid(query.getStartId())
				|| YitunStringUtil.isEmpty(query.getDirection()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		return groupChatContentService.getListByStartId(query);
	}

	/**
	 * Description 获取最近聊天列表（含最后聊天内容）<br>
	 * 此接口包含聊天对象详细信息和聊天内容<br>
	 * Date 2019-05-25 08:44:15<br>
	 * @param head
//	 * @param socketSession
	 * @param query
	 * @return Object
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0007")
	public Object getLastChatWithContentList(HeadImpl head,
			SocketSession socketSession,
			@JsonDefine("data") GroupChatContentDto query) {
//		userId = socketSession.getKey();
		Long userId = query.getUserId();
		List<LastChatGroup> list = lastChatGroupService.getLastChatWithContentList(userId, query.getCurrentPageNo(), query.getPageSize());
		return new ResultMessage(R.param("list", list));
	}

	/**
	 * Description 获取聊天记录（此方法主要以消息key为条件）
	 * Date 2019-05-25 08:05:13<br>
	 * @param head
	 * @return Object
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0008")
	public Object getListByStartMessageKey(HeadImpl head,
										 @JsonDefine("data") GroupChatContentDto query) {
		if (NumUtil.isInValid(query.getGroupId()) || YitunStringUtil.isEmpty(query.getDirection()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		return groupChatContentService.getListByStartMessageKey(query);
	}
}
